package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public ResultModel findAll(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel add(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel findById(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel update(CompanyModel companyModel) {

        CompanyDao companyDao = new CompanyImpl();
        int update = companyDao.update(companyModel);
        Jedis jedis = new Jedis();
        if (update > 0) {
            jedis.del("logo");
        }
        ResultModel resultModel = new ResultModel();
        resultModel.setCount(update);
        return resultModel;
    }

    @Override
    public ResultModel delete(CompanyModel companyModel) {

        CompanyDao companyDao = new CompanyImpl();
        int update = companyDao.delete(companyModel);
        Jedis jedis = new Jedis();
        if (update > 0) {
            jedis.del("logo");
        }
        ResultModel resultModel = new ResultModel();
        resultModel.setCount(update);
        return resultModel;
    }

    @Override
    public ResultModel getCount(CompanyModel companyModel) {
        return null;
    }

    /**
     * 1.看redis里面是否有数据
     * 2.如果没有，则取数据库数据，放入到缓存
     * 3.如果有，则直接取缓存数据
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
//        怎么得到redis？
        Jedis jedis = new Jedis();
//        判断是否有值？
        Boolean exists = jedis.exists("logo");
        CompanyModel companyModel = new CompanyModel();
        if (exists) {
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
        } else {
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
//            jedis.set("logo", companyModel.getLogo());
//            jedis.expire("logo",60*30);//自动删除redis数据
            jedis.setex("logo", 30 * 60, companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.expire("phone", 60 * 30);//自动删除redis数据
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("address", companyModel.getAddress());
            jedis.set("version", companyModel.getVersion());
        }

        return companyModel;
    }
}
